package com.whu.dao.interfaces;

import com.whu.aircraft.Location;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface RoutesDao {

    @Insert("INSERT INTO air(physical_address, air_name) VALUES(#{physical_address}, #{air_name})")
    void addAir(@Param("physical_address") int physical_address, @Param("air_name") String air_name);

    @Select("SELECT COUNT(air_id) FROM air")
    int selectAirCount();

    @Select("SELECT air_id FROM air WHERE air_name = #{air_name}")
    int selectAirId(@Param("air_name") String air_name);

    @Select("SELECT count(route_name) from air_route WHERE air_id = #{air_id}")
    int selectRoutesCount(@Param("air_id") int air_id);

    @Insert("INSERT INTO air_route(air_id, route_name, route_id) VALUES(#{air_id}, #{route_name}, #{route_id})")
    void createRoute(@Param("air_id") int air_id, @Param("route_name") String route_name, @Param("route_id") String route_id);

    @Insert("CREATE TABLE ${table_name}(longitude DOUBLE(13, 7), latitude DOUBLE(13, 7), height DOUBLE(13, 7))")
    void createRouteTable(String table_name);

    @Insert({
            "<script>",
            "insert into ${table_name}(longitude, latitude, height) values ",
            "<foreach collection='routePoints' item='point' separator=','>",
            "(#{point.longitude}, #{point.latitude}, #{point.height})",
            "</foreach>",
            "</script>"
    })
    void insertRoutePoints(String table_name, List<Location> routePoints);

    @Select("SELECT route_id FROM air_route WHERE air_id = #{air_id} AND route_name = #{route_name}")
    String selectRouteId(int air_id, String route_name);

}
